Imports System.Xml.Serialization
Imports System.IO
Imports ToDoList.Navigation
Imports System.Xml

Namespace Usecases
  ''' <summary>
  ''' Usecase: user selects a file to show as a todolist
  ''' </summary>
  ''' <remarks></remarks>
  Public Class SelectTodoListUsecase

    Private mMyNavigation As ISelectToDoListNavigation

    'dependent usecases
    Private mManageUsecase As ManageTodoListUsecase

    'state
    Private mFileName As String = String.Empty

    ''' <summary>
    ''' Constructor
    ''' </summary>
    ''' <param name="navigation">view for this usecase</param>
    Public Sub New(ByVal navigation As ISelectToDoListNavigation, ByVal manageUsecase As ManageTodoListUsecase)
      mMyNavigation = navigation
      mManageUsecase = manageUsecase
    End Sub

    ''' <summary>
    ''' Start this usecase
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Start()
      'present a selection choice: either make a new
      'list or open an existing list
      mMyNavigation.PresentSelectionChoice()
    End Sub

    ''' <summary>
    ''' user wants to begin a new todolist
    ''' </summary>
    Public Sub RequestNew()
      'fine 
      Dim lTodoList As New Model.Todolist
      mManageUsecase.Start(lTodoList)
    End Sub

    ''' <summary>
    ''' User has selected a file
    ''' </summary>
    ''' <param name="fileName">path to the file</param>
    Public Sub FileToOpenSelected(ByVal fileName As String)
      mFileName = fileName

      'start dependent usecase
      Dim lTodoList As Model.Todolist = readFile()
      If lTodoList IsNot Nothing Then
        mManageUsecase.Start(lTodoList)
      End If

    End Sub

    ''' <summary>
    ''' user requests to save the file
    ''' </summary>
    ''' <remarks>will be a save as effectively if the file has not been saved before</remarks>
    Public Sub RequestSave()
      If mFileName.Length = 0 Then
        RequestSaveAs()
      Else
        saveFile(mManageUsecase.TodoList)
      End If
    End Sub

    ''' <summary>
    ''' User requests to save the file "as"
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub RequestSaveAs()
      mMyNavigation.AskFileName()
    End Sub

    ''' <summary>
    ''' user has selected a fileaname to save the file with
    ''' </summary>
    Public Sub FileNameToSaveEntered(ByVal filename As String)
      mFileName = filename
      saveFile(mManageUsecase.TodoList)
    End Sub

    ''' <summary>
    ''' Read the todolist in memory
    ''' </summary>
    Private Function readFile() As Model.Todolist

      Dim lTodolist As Model.Todolist = Nothing

      'deserialize the XML document into our model
      Try
        Using lReader As TextReader = File.OpenText(mFileName)

          Dim lSerializer As New XmlSerializer(GetType(Model.Todolist))
          lTodolist = CType(lSerializer.Deserialize(lReader), Model.Todolist)
        End Using
        'success
      Catch ex As Exception
        mMyNavigation.Message("Please select a valid ToDoList file")
      End Try

      Return lTodolist
    End Function

    Private Sub saveFile(ByVal todoList As Model.Todolist)
      Try
        Using lWriter As TextWriter = New StreamWriter(File.OpenWrite(mFileName))
          Dim lSerializer As New XmlSerializer(GetType(Model.Todolist))
          lSerializer.Serialize(lWriter, todoList)
        End Using
      Catch ex As Exception
        mMyNavigation.Message(String.Format("There was an error during save: {0}", ex.Message))
      End Try
    End Sub

    ''' <summary>
    ''' Public state: path to the todolist file
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property TodoListPath() As String
      Get
        Return mFileName
      End Get
    End Property

  End Class
End Namespace

